/*
Copyright 2011 Erick Martínez Sámano
This file is part of PHP Quick Calendar E.
*/
var transAlerts= new Class({
	Implements:Options,
	options:{
		en:{
			DOC_NOT_FOUND:"Document not found",
			T_W_E_P_R:"There was an error processing the request",
			P_Try_A_L:"Please try again later"
		},
		es:{
			DOC_NOT_FOUND:"Documento no encontrado",
			T_W_E_P_R:"Hubo un error al procesar su petición",
			P_Try_A_L:"Por favor, intente de nuevo más tarde"
		},
		nl:{
			DOC_NOT_FOUND:"Document niet gevonden",
			T_W_E_P_R:"Er is een fout opgetreden bij het verwerken van de aanvraag",
			P_Try_A_L:"Probeert u het later nog eens"
		}
	},
	define: function(){
		ltag=document.getElement('html').lang.substr(0,2);
		(typeof(this.options[ltag])=="undefined")?ltag="en":'';
		this.alerts=this.options[ltag];
	},
	getMsgs: function(){
		(!this.alerts)? this.define():'';
		return this.alerts;
	}
});
var smallReq = new Class({
	Implements: [Events,Options],
	initialize: function(container,target){
		if (typeof(target)=='undefined') {target=container;}
		target.set('tween',{duration:500,onStart:this.ejecutar.bind(this)});
		if (Browser.ie){container.setStyle('zoom',1);}
		this.xhrqe=new Request.HTML({
		onRequest: function(){
				(this.uerr)? this.uerr.setStyle('display','none'):'';
				(this.err404)? this.err404.setStyle('display','none'):'';
				(this.seErr)? this.seErr.setStyle('display','none'):'';
				target.fade('hide');
				container.addClass("QCEloader");
			},
		onComplete: function(){
				container.removeClass.delay(20,container,"QCEloader");
				target.fade('show');
			},
		onFailure: function(xhr){if (xhr.status==404){
						if (!this.err404){
							this.err404=new Element('p.QCEErr',{html:this.alerts.DOC_NOT_FOUND}); container.grab(this.err404,'top');} (this.uerr)? this.uerr.setStyle('display','none'):''; this.err404.setStyle('display','block');
						} else {
						if (!this.uerr){
							this.uerr=new Element('p.QCEErr',{html:this.alerts.T_W_E_P_R});container.grab(this.uerr,'top');} (this.err404)? this.err404.setStyle('display','none'):'';this.uerr.setStyle('display','block');
						}
						container.fireEvent('QCEAjErr',target);
			},
		onSuccess: function(tree,elements,html,js){
				if (this.getHeader('pqce')=='t'){
					target.set('html', html);
					this.js=js;
					target.tween('opacity',1);
				}else{  if(!this.seErr){
					this.seErr=new Element('p.QCEErr',{html:this.alerts.P_Try_A_L});
					container.grab(this.seErr,'top');}
					container.fireEvent('QCEAjErr',target);
					this.seErr.setStyle('display','block');
				}
			},
		evalScripts:false,
		chain:'ignore'
		});
	var al= new transAlerts();
	this.xhrqe.alerts=al.getMsgs();
	al=null;
	},
	send: function(url){
		this.xhrqe.options.url=url;
		this.xhrqe.send();
	},
	ejecutar: function(){
		$exec(this.xhrqe.js);
	}
});
var smallShow = new Class({
	Implements: [Options, Events],
	options:{
		espacio:50,
		duracion:500,
		fps:50
	},
	initialize: function(prev,next,close,padre,options){
		this.ie= (Browser.ie)? 'quad:out':'quint:out';
		this.setOptions(options);
		this.padre=padre;
		this.prev=prev;
		this.next=next;
		this.padreTween=new Fx.Tween(padre,{property:'opacity', duration:2000,transition:this.ie,link:'cancel',onComplete: (function(){
			if (!this.padre.getStyle('opacity').toInt()){this.padre.setStyle('display','none');this.prev.setStyle('visibility','hidden');}
							}).bind(this)
				});
		this.padre.addEvent('mouseleave',this.ocultar.bind(this));
		this.padre.addEvent('mouseenter',this.mostrar.bind(this));
		close.addEvent('click',function(){
			padre.setStyles({'display':'none','opacity':0});
			prev.setStyle('visibility','hidden');
			next.setStyle('visibility','hidden');
		});
		prev.addEvent('click',this.back.bind(this));
		next.addEvent('click',this.forw.bind(this));

	},
	scan: function(target){
		this.children=target.getChildren('div');
		this.last=this.children.length-1;
		if (this.last<2){return;}
			this.next.setStyle('visibility','visible');
			this.tween= new Fx.Tween(target,{property:'left', duration:this.options.duracion,transition:this.ie,fps:this.options.fps});
			this.left=this.children[0].getSize().x;
			this.onefx=false;
			this.target=target;
			this.visibleChild=0;
	},
	forw: function(){
		if (this.last<2){return;}
		if (this.onefx){return;}
		this.onefx=true;
		this.children[this.visibleChild+2].setStyle('display','block');
		this.tween.start(0,-(this.left + this.options.espacio)).chain((function(){
		this.children[this.visibleChild].setStyle('display','none');
		this.children[(this.visibleChild+1)].setStyle('display','none');
		this.tween.set(0);
		this.prev.setStyle('visibility','visible');
		this.visibleChild+=2;
		this.onefx=false;
		if (this.last==this.visibleChild) {this.next.setStyle('visibility','hidden');}
		}).bind(this));
		
	},
	back: function(){
		if (this.last<2){return;}
		if (this.onefx){return;}
		this.onefx=true;
		this.children[(this.visibleChild-1)].setStyle('display','block');
		this.tween.set(-this.options.espacio);
		this.children[(this.visibleChild-2)].setStyle('display','block');
		this.tween.set(-(this.options.espacio+this.left));
		this.tween.start(0).chain((function(){
		this.next.setStyle('visibility','visible');
		this.children[this.visibleChild].setStyle('display','none');
		this.visibleChild-=2;
		this.onefx=false;
		if (this.visibleChild==0) {this.prev.setStyle('visibility','hidden');}
		}).bind(this));		
	},
	ocultar: function(){
		this.timer=this.padreTween.start.delay(10000,this.padreTween,0);
	},
	mostrar: function(){
		clearTimeout(this.timer);
		this.padreTween.start(1);
	}
});

window.addEvent('domready',function(){
	smallQCEreq= new smallReq(document.id('QCEsmall'),document.id('QCEsmalltrgt'));
	var el=document.id("QCEsmalltrgt");
	var target= document.id('smallQCEtrgt');
	var target1= document.id('smallSmooth');
	smallQCETween= new smallShow(document.id("smallQCEa"),document.id("smallQCEs"),document.id("smallQCEc"),document.id("smallQCEtrgt"),{duracion:1500,fps:40});
	el.addEvent('click:relay(.Apqcejx)',function(e){
		e.stop();
		target.setStyle('display','none');target1.setStyle('opacity',0);
		if (this.retrieve('Ahref')==null){
		var ref=this.href;
		var c= (ref.indexOf('?')+1)? '&': '?';
		ref+=c+'view=small&format=raw';
		this.store('Ahref',ref);
		}
		pqceHideArr();
		smallQCEreq.send(this.retrieve('Ahref'));
	});
	smallQCEreqB= new smallReq(target,target1);
	el.addEvent('click:relay(.pQCEsmall)',function(e){
		e.stop();
		pqceHideArr();
		target.setStyles({'display':'block','visibility':'visible','opacity':1});
		if (this.retrieve('Ahref')==null){
		var ref=this.href;
		var c= (ref.indexOf('?')+1)? '&': '?';
		ref+=c+'view=small&format=raw&p=desc';
		this.store('Ahref',ref);
		}
		$clear(smallQCETween.timer);
		smallQCETween.padreTween.cancel();
		smallQCEreqB.send(this.retrieve('Ahref'));
	});
	target.addEvent('QCEAjErr',function(el){
		el.setStyle('visibility','hidden');
		pqceHideArr();
	});
});
function pqceHideArr(){
	document.id("smallQCEa").setStyle('visibility','hidden');
	document.id("smallQCEs").setStyle('visibility','hidden');
}
